package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 养修服务单项控制器
 */
@Controller
@RequestMapping("business/bpmnInfo")
public class BpmnInfoController {
    //模板前缀
    private static final String prefix = "business/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String list(){
        System.out.println("123");
        return prefix + "list";
    }

    @RequiresPermissions("business:bpmnInfo:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequiresPermissions("business:bpmnInfo:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("bpmnInfo", bpmnInfoService.getById(id));
        return prefix + "edit";
    }

    //流程定义部署
    @RequestMapping("/deployPage")
    public String deployPage(Model model,Long id){
        model.addAttribute("bpmnInfo", bpmnInfoService.getById(id));
        return prefix + "deploy";
    }


    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo query(BpmnInfoQuery qo){
        return bpmnInfoService.query(qo);
    }

    //新增
    @RequiresPermissions("business:bpmnInfo:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BpmnInfo bpmnInfo){
        bpmnInfoService.save(bpmnInfo);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:bpmnInfo:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(BpmnInfo bpmnInfo){
        bpmnInfoService.updateById(bpmnInfo);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:bpmnInfo:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        bpmnInfoService.removeById(ids);
        return AjaxResult.success();
    }
    //流程定义文件上传
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        String upload = null;
        //查询并获取得到后缀名的对象
        String extension = FileUploadUtils.getExtension(file);
        //判断后缀名不为空并且文件的内容不为空
        if (extension != null && file.getSize() > 0){
            //如果存在后缀名为bpmn的文件则进行加载
            if ("bpmn".equals(extension)) {
                upload = FileUploadUtils.upload(SystemConfig.getUploadPath(), file);
            }else {
                throw new BusinessException("流程定义只支持bpmn格式文件");
            }
        }else {
            throw new BusinessException("文件上传不能为空！");
        }
        return AjaxResult.success("文件上传成功",upload);
    }
    //流程部署下载
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(String path,String bpmnType,String info) throws FileNotFoundException {
        bpmnInfoService.deploy(path,bpmnType,info);
        return AjaxResult.success();
    }


    //删除流程
    //流程部署
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id) {
        bpmnInfoService.deleteDeployment(id);
        return AjaxResult.success();
    }


    //bpmn文件与视图
    @RequestMapping("/readResource")
    @ResponseBody
    public void readResource(String deploymentId, String type, HttpServletResponse response) throws IOException {
        InputStream inputStream = bpmnInfoService.readResourceOrType(deploymentId, type);
        IOUtils.copy(inputStream,response.getOutputStream());

    }

}
